home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / domacnost a kancelar / autoit / autoit-v3-setup.exe / Include / Date.au3 < prev    next >
Encoding:
Text File  |  2007-09-08  |  60.9 KB  |  1,624 lines

  1. #include-once
  2. ;
  3. ; ------------------------------------------------------------------------------
  4. ;
  5. ; AutoIt Version: 3.0
  6. ; Language:       English
  7. ; Description:    Functions that assist with dates and times.
  8. ;
  9. ;===============================================================================
  10. ;
  11. ; Description:      Calculates a new date based on a given date and add an interval.
  12. ; Parameter(s):     $sType    D = Add number of days to the given date
  13. ;                             M = Add number of months to the given date
  14. ;                             Y = Add number of years to the given date
  15. ;                             w = Add number of Weeks to the given date
  16. ;                             h = Add number of hours to the given date
  17. ;                             n = Add number of minutes to the given date
  18. ;                             s = Add number of seconds to the given date
  19. ;                   $iValToAdd - number to be added
  20. ;                   $sDate    - Input date in the format YYYY/MM/DD[ HH:MM:SS]
  21. ; Requirement(s):   None
  22. ; Return Value(s):  On Success - Date newly calculated date.
  23. ;                   On Failure - 0  and Set
  24. ;                                   @ERROR to:  1 - Invalid $sType
  25. ;                                                  2 - Invalid $iValToAdd
  26. ;                                                  3 - Invalid $sDate
  27. ; Author(s):        Jos van der Zande
  28. ; Note(s):          The function will not return an invalid date.
  29. ;                   When 3 months is are to '2004/1/31' then the result will be 2004/04/30
  30. ;
  31. ;
  32. ;===============================================================================
  33. Func _DateAdd($sType, $iValToAdd, $sDate)
  34.     Local $asTimePart[4]
  35.     Local $asDatePart[4]
  36.     Local $iJulianDate
  37.     Local $iTimeVal
  38.     Local $iNumDays
  39.     Local $Day2Add
  40.     ; Verify that $sType is Valid
  41.     $sType = StringLeft($sType, 1)
  42.     If StringInStr("D,M,Y,w,h,n,s", $sType) = 0 Or $sType = "" Then
  43.         SetError(1)
  44.         Return (0)
  45.     EndIf
  46.     ; Verify that Value to Add  is Valid
  47.     If Not StringIsInt($iValToAdd) Then
  48.         SetError(2)
  49.         Return (0)
  50.     EndIf
  51.     ; Verify If InputDate is valid
  52.     If Not _DateIsValid($sDate) Then
  53.         SetError(3)
  54.         Return (0)
  55.     EndIf
  56.     ; split the date and time into arrays
  57.     _DateTimeSplit($sDate, $asDatePart, $asTimePart)
  58.     
  59.     ; ====================================================
  60.     ; adding days then get the julian date
  61.     ; add the number of day
  62.     ; and convert back to Gregorian
  63.     If $sType = "d" Or $sType = "w" Then
  64.         If $sType = "w" Then $iValToAdd = $iValToAdd * 7
  65.         $iJulianDate = _DateToDayValue($asDatePart[1], $asDatePart[2], $asDatePart[3]) + $iValToAdd
  66.         _DayValueToDate($iJulianDate, $asDatePart[1], $asDatePart[2], $asDatePart[3])
  67.     EndIf
  68.     ; ====================================================
  69.     ; adding Months
  70.     If $sType = "m" Then
  71.         $asDatePart[2] = $asDatePart[2] + $iValToAdd
  72.         ; pos number of months
  73.         While $asDatePart[2] > 12
  74.             $asDatePart[2] = $asDatePart[2] - 12
  75.             $asDatePart[1] = $asDatePart[1] + 1
  76.         WEnd
  77.         ; Neg number of months
  78.         While $asDatePart[2] < 1
  79.             $asDatePart[2] = $asDatePart[2] + 12
  80.             $asDatePart[1] = $asDatePart[1] - 1
  81.         WEnd
  82.     EndIf
  83.     ; ====================================================
  84.     ; adding Years
  85.     If $sType = "y" Then
  86.         $asDatePart[1] = $asDatePart[1] + $iValToAdd
  87.     EndIf
  88.     ; ====================================================
  89.     ; adding Time value
  90.     If $sType = "h" Or $sType = "n" Or $sType = "s" Then
  91.         $iTimeVal = _TimeToTicks($asTimePart[1], $asTimePart[2], $asTimePart[3]) / 1000
  92.         If $sType = "h" Then $iTimeVal = $iTimeVal + $iValToAdd * 3600
  93.         If $sType = "n" Then $iTimeVal = $iTimeVal + $iValToAdd * 60
  94.         If $sType = "s" Then $iTimeVal = $iTimeVal + $iValToAdd
  95.         ; calculated days to add
  96.         $Day2Add = Int($iTimeVal/ (24 * 60 * 60))
  97.         $iTimeVal = $iTimeVal - $Day2Add * 24 * 60 * 60
  98.         If $iTimeVal < 0 Then
  99.             $Day2Add = $Day2Add - 1
  100.             $iTimeVal = $iTimeVal + 24 * 60 * 60
  101.         EndIf
  102.         $iJulianDate = _DateToDayValue($asDatePart[1], $asDatePart[2], $asDatePart[3]) + $Day2Add
  103.         ; calculate the julian back to date
  104.         _DayValueToDate($iJulianDate, $asDatePart[1], $asDatePart[2], $asDatePart[3])
  105.         ; caluculate the new time
  106.         _TicksToTime($iTimeVal * 1000, $asTimePart[1], $asTimePart[2], $asTimePart[3])
  107.     EndIf
  108.     ; ====================================================
  109.     ; check if the Input day is Greater then the new month last day.
  110.     ; if so then change it to the last possible day in the month
  111.     $iNumDays = StringSplit('31,28,31,30,31,30,31,31,30,31,30,31', ',')
  112.     If _DateIsLeapYear($asDatePart[1]) Then $iNumDays[2] = 29
  113.     ;
  114.     If $iNumDays[$asDatePart[2]] < $asDatePart[3] Then $asDatePart[3] = $iNumDays[$asDatePart[2]]
  115.     ; ========================
  116.     ; Format the return date
  117.     ; ========================
  118.     ; Format the return date
  119.     $sDate = $asDatePart[1] & '/' & StringRight("0" & $asDatePart[2], 2) & '/' & StringRight("0" & $asDatePart[3], 2)
  120.     ; add the time when specified in the input
  121.     If $asTimePart[0] > 0 Then
  122.         If $asTimePart[0] > 2 Then
  123.             $sDate = $sDate & " " & StringRight("0" & $asTimePart[1], 2) & ':' & StringRight("0" & $asTimePart[2], 2) & ':' & StringRight("0" & $asTimePart[3], 2)
  124.         Else
  125.             $sDate = $sDate & " " & StringRight("0" & $asTimePart[1], 2) & ':' & StringRight("0" & $asTimePart[2], 2)
  126.         EndIf
  127.     EndIf
  128.     ;
  129.     Return ($sDate)
  130. EndFunc   ;==>_DateAdd
  131.  
  132. ;===============================================================================
  133. ;
  134. ; Description:      Returns the name of the weekday, based on the specified day.
  135. ; Parameter(s):     $iDayNum - Day number
  136. ;                   $iShort  - Format:
  137. ;                              0 = Long name of the weekday
  138. ;                              1 = Abbreviated name of the weekday
  139. ; Requirement(s):   None
  140. ; Return Value(s):  On Success - Weekday name
  141. ;                   On Failure - A NULL string and sets @ERROR = 1
  142. ; Author(s):        Jeremy Landes <jlandes at landeserve dot com>
  143. ; Note(s):          English only
  144. ;
  145. ;===============================================================================
  146. Func _DateDayOfWeek($iDayNum, $iShort = 0)
  147.     ;==============================================
  148.     ; Local Constant/Variable Declaration Section
  149.     ;==============================================
  150.     Local $aDayOfWeek[8]
  151.     
  152.     $aDayOfWeek[1] = "Sunday"
  153.     $aDayOfWeek[2] = "Monday"
  154.     $aDayOfWeek[3] = "Tuesday"
  155.     $aDayOfWeek[4] = "Wednesday"
  156.     $aDayOfWeek[5] = "Thursday"
  157.     $aDayOfWeek[6] = "Friday"
  158.     $aDayOfWeek[7] = "Saturday"
  159.     Select
  160.         Case Not StringIsInt($iDayNum) Or Not StringIsInt($iShort)
  161.             SetError(1)
  162.             Return ""
  163.         Case $iDayNum < 1 Or $iDayNum > 7
  164.             SetError(1)
  165.             Return ""
  166.         Case Else
  167.             Select
  168.                 Case $iShort = 0
  169.                     Return $aDayOfWeek[$iDayNum]
  170.                 Case $iShort = 1
  171.                     Return StringLeft($aDayOfWeek[$iDayNum], 3)
  172.                 Case Else
  173.                     SetError(1)
  174.                     Return ""
  175.             EndSelect
  176.     EndSelect
  177. EndFunc   ;==>_DateDayOfWeek
  178.  
  179. ;===============================================================================
  180. ;
  181. ; Function Name:  _DateDaysInMonth()
  182. ; Description:    Returns the number of days in a month, based on the specified
  183. ;                 month and year.
  184. ; Author(s):      Jeremy Landes <jlandes at landeserve dot com>
  185. ;
  186. ;===============================================================================
  187. Func _DateDaysInMonth($iYear, $iMonthNum)
  188.     Local $aiNumDays
  189.     $aiNumDays = "31,28,31,30,31,30,31,31,30,31,30,31"
  190.     $aiNumDays = StringSplit($aiNumDays, ",")
  191.     If _DateIsMonth($iMonthNum) And _DateIsYear($iYear) Then
  192.         If _DateIsLeapYear($iYear) Then $aiNumDays[2] = $aiNumDays[2] + 1
  193.         SetError(0)
  194.         Return $aiNumDays[$iMonthNum]
  195.     Else
  196.         SetError(1)
  197.         Return 0
  198.     EndIf
  199. EndFunc   ;==>_DateDaysInMonth
  200.  
  201. ;===============================================================================
  202. ;
  203. ; Description:      Returns the difference between 2 dates, expressed in the type requested
  204. ; Parameter(s):     $sType - returns the difference in:
  205. ;                               d = days
  206. ;                               m = Months
  207. ;                               y = Years
  208. ;                               w = Weeks
  209. ;                               h = Hours
  210. ;                               n = Minutes
  211. ;                               s = Seconds
  212. ;                   $sStartDate    - Input Start date in the format "YYYY/MM/DD[ HH:MM:SS]"
  213. ;                   $sEndDate    - Input End date in the format "YYYY/MM/DD[ HH:MM:SS]"
  214. ; Requirement(s):   None
  215. ; Return Value(s):  On Success - Difference between the 2 dates
  216. ;                   On Failure - 0  and Set
  217. ;                                   @ERROR to:  1 - Invalid $sType
  218. ;                                               2 - Invalid $sStartDate
  219. ;                                               3 - Invalid $sEndDate
  220. ; Author(s):        Jos van der Zande
  221. ; Note(s):
  222. ;
  223. ;===============================================================================
  224. Func _DateDiff($sType, $sStartDate, $sEndDate)
  225.     Local $asStartDatePart[4]
  226.     Local $asStartTimePart[4]
  227.     Local $asEndDatePart[4]
  228.     Local $asEndTimePart[4]
  229.     Local $iTimeDiff
  230.     Local $iYearDiff
  231.     Local $iMonthDiff
  232.     Local $iStartTimeInSecs
  233.     Local $iEndTimeInSecs
  234.     Local $aDaysDiff
  235.     ;
  236.     ; Verify that $sType is Valid
  237.     $sType = StringLeft($sType, 1)
  238.     If StringInStr("d,m,y,w,h,n,s", $sType) = 0 Or $sType = "" Then
  239.         SetError(1)
  240.         Return (0)
  241.     EndIf
  242.     ; Verify If StartDate is valid
  243.     If Not _DateIsValid($sStartDate) Then
  244.         SetError(2)
  245.         Return (0)
  246.     EndIf
  247.     ; Verify If EndDate is valid
  248.     If Not _DateIsValid($sEndDate) Then
  249.         SetError(3)
  250.         Return (0)
  251.     EndIf
  252.     ; split the StartDate and Time into arrays
  253.     _DateTimeSplit($sStartDate, $asStartDatePart, $asStartTimePart)
  254.     ; split the End  Date and time into arrays
  255.     _DateTimeSplit($sEndDate, $asEndDatePart, $asEndTimePart)
  256.     ; ====================================================
  257.     ; Get the differens in days between the 2 dates
  258.     $aDaysDiff = _DateToDayValue($asEndDatePart[1], $asEndDatePart[2], $asEndDatePart[3]) - _DateToDayValue($asStartDatePart[1], $asStartDatePart[2], $asStartDatePart[3])
  259.     ; ====================================================
  260.     ; Get the differens in Seconds between the 2 times when specified
  261.     If $asStartTimePart[0] > 1 And $asEndTimePart[0] > 1 Then
  262.         $iStartTimeInSecs = $asStartTimePart[1] * 3600 + $asStartTimePart[2] * 60 + $asStartTimePart[3]
  263.         $iEndTimeInSecs = $asEndTimePart[1] * 3600 + $asEndTimePart[2] * 60 + $asEndTimePart[3]
  264.         $iTimeDiff = $iEndTimeInSecs - $iStartTimeInSecs
  265.         If $iTimeDiff < 0 Then
  266.             $aDaysDiff = $aDaysDiff - 1
  267.             $iTimeDiff = $iTimeDiff + 24 * 60 * 60
  268.         EndIf
  269.     Else
  270.         $iTimeDiff = 0
  271.     EndIf
  272.     Select
  273.         Case $sType = "d"
  274.             Return ($aDaysDiff)
  275.         Case $sType = "m"
  276.             $iYearDiff = $asEndDatePart[1] - $asStartDatePart[1]
  277.             $iMonthDiff = $asEndDatePart[2] - $asStartDatePart[2] + $iYearDiff * 12
  278.             If $asEndDatePart[3] < $asStartDatePart[3] Then $iMonthDiff = $iMonthDiff - 1
  279.             $iStartTimeInSecs = $asStartTimePart[1] * 3600 + $asStartTimePart[2] * 60 + $asStartTimePart[3]
  280.             $iEndTimeInSecs = $asEndTimePart[1] * 3600 + $asEndTimePart[2] * 60 + $asEndTimePart[3]
  281.             $iTimeDiff = $iEndTimeInSecs - $iStartTimeInSecs
  282.             If $asEndDatePart[3] = $asStartDatePart[3] And $iTimeDiff < 0 Then $iMonthDiff = $iMonthDiff - 1
  283.             Return ($iMonthDiff)
  284.         Case $sType = "y"
  285.             $iYearDiff = $asEndDatePart[1] - $asStartDatePart[1]
  286.             If $asEndDatePart[2] < $asStartDatePart[2] Then $iYearDiff = $iYearDiff - 1
  287.             If $asEndDatePart[2] = $asStartDatePart[2] And $asEndDatePart[3] < $asStartDatePart[3] Then $iYearDiff = $iYearDiff - 1
  288.             $iStartTimeInSecs = $asStartTimePart[1] * 3600 + $asStartTimePart[2] * 60 + $asStartTimePart[3]
  289.             $iEndTimeInSecs = $asEndTimePart[1] * 3600 + $asEndTimePart[2] * 60 + $asEndTimePart[3]
  290.             $iTimeDiff = $iEndTimeInSecs - $iStartTimeInSecs
  291.             If $asEndDatePart[2] = $asStartDatePart[2] And $asEndDatePart[3] = $asStartDatePart[3] And $iTimeDiff < 0 Then $iYearDiff = $iYearDiff - 1
  292.             Return ($iYearDiff)
  293.         Case $sType = "w"
  294.             Return (Int($aDaysDiff / 7))
  295.         Case $sType = "h"
  296.             Return ($aDaysDiff * 24 + Int($iTimeDiff / 3600))
  297.         Case $sType = "n"
  298.             Return ($aDaysDiff * 24 * 60 + Int($iTimeDiff / 60))
  299.         Case $sType = "s"
  300.             Return ($aDaysDiff * 24 * 60 * 60 + $iTimeDiff)
  301.     EndSelect
  302. EndFunc   ;==>_DateDiff
  303.  
  304. ;===============================================================================
  305. ;
  306. ; Description:      Returns 1 if the specified year falls on a leap year and
  307. ;                   returns 0 if it does not.
  308. ; Parameter(s):     $iYear - Year to check
  309. ; Requirement(s):   None
  310. ; Return Value(s):  On Success - 0 = Year is not a leap year
  311. ;                                1 = Year is a leap year
  312. ;                   On Failure - 0 and sets @ERROR = 1
  313. ; Author(s):        Jeremy Landes <jlandes at landeserve dot com>
  314. ; Note(s):          None
  315. ;
  316. ;===============================================================================
  317. Func _DateIsLeapYear($iYear)
  318.     If StringIsInt($iYear) Then
  319.         Select
  320.             Case Mod($iYear, 4) = 0 And Mod($iYear, 100) <> 0
  321.                 Return 1
  322.             Case Mod($iYear, 400) = 0
  323.                 Return 1
  324.             Case Else
  325.                 Return 0
  326.         EndSelect
  327.     Else
  328.         SetError(1)
  329.         Return 0
  330.     EndIf
  331. EndFunc   ;==>_DateIsLeapYear
  332.  
  333. ;===============================================================================
  334. ;
  335. ; Function Name:  _DateIsMonth()
  336. ; Description:    Checks a given number to see if it is a valid month.
  337. ; Author(s):      Jeremy Landes <jlandes at landeserve dot com>
  338. ;
  339. ;===============================================================================
  340. Func _DateIsMonth($iNumber)
  341.     If StringIsInt($iNumber) Then
  342.         If $iNumber >= 1 And $iNumber <= 12 Then
  343.             Return 1
  344.         Else
  345.             Return 0
  346.         EndIf
  347.     Else
  348.         Return 0
  349.     EndIf
  350. EndFunc   ;==>_DateIsMonth
  351.  
  352. ;===============================================================================
  353. ;
  354. ; Description:      Verify if date and time are valid "yyyy/mm/dd[ hh:mm[:ss]]".
  355. ; Parameter(s):     $sDate format "yyyy/mm/dd[ hh:mm[:ss]]"
  356. ; Requirement(s):   None
  357. ; Return Value(s):  On Success - 1
  358. ;                   On Failure - 0
  359. ; Author(s):        Jeremy Landes <jlandes at landeserve dot com>
  360. ;                   Jos van der Zande <jdeb at autoitscript dot com>
  361. ; Note(s):          None
  362. ;
  363. ;===============================================================================
  364. Func _DateIsValid($sDate)
  365.     Local $asDatePart[4]
  366.     Local $asTimePart[4]
  367.     Local $iNumDays
  368.     Local $sDateTime
  369.     $iNumDays = "31,28,31,30,31,30,31,31,30,31,30,31"
  370.     $iNumDays = StringSplit($iNumDays, ",")
  371.     ; split the Date and Time portion
  372.     $sDateTime = StringSplit($sDate, " T")
  373.     ; split the date portion
  374.     If $sDateTime[0] > 0 Then $asDatePart = StringSplit($sDateTime[1], "/-.")
  375.     ; Ensure the date contains 3 sections YYYY MM DD
  376.     If UBound($asDatePart) <> 4 Then Return(0)
  377.     If $asDatePart[0] <> 3 Then Return (0)
  378.     ; verify valid input date values
  379.     ; Make sure the Date parts contains numeric
  380.     If Not StringIsInt($asDatePart[1]) Then Return (0)
  381.     If Not StringIsInt($asDatePart[2]) Then Return (0)
  382.     If Not StringIsInt($asDatePart[3]) Then Return (0)
  383.     $asDatePart[1] = Number($asDatePart[1])
  384.     $asDatePart[2] = Number($asDatePart[2])
  385.     $asDatePart[3] = Number($asDatePart[3])
  386.     ; check if all contain valid values
  387.     If _DateIsLeapYear($asDatePart[1]) Then $iNumDays[2] = 29
  388.     If $asDatePart[1] < 1000 Or $asDatePart[1] > 2999 Then Return (0)
  389.     If $asDatePart[2] < 1 Or $asDatePart[2] > 12 Then Return (0)
  390.     If $asDatePart[3] < 1 Or $asDatePart[3] > $iNumDays[$asDatePart[2]] Then Return (0)
  391.     ; split the Time portion
  392.     If $sDateTime[0] > 1 Then
  393.         $asTimePart = StringSplit($sDateTime[2], ":")
  394.         If UBound($asTimePart) < 4 Then ReDim $asTimePart[4]
  395.     Else
  396.         Dim $asTimePart[4]
  397.     EndIf
  398.     ; check Time portion
  399.     If $asTimePart[0] < 1 Then Return (1)             ; No time specified so date must be correct
  400.     If $asTimePart[0] < 2 Then Return (0)             ; need at least HH:MM when something is specified
  401.     If $asTimePart[0] = 2 Then $asTimePart[3] = "00"  ; init SS when only HH:MM is specified
  402.     ; Make sure the Time parts contains numeric
  403.     If Not StringIsInt($asTimePart[1]) Then Return (0)
  404.     If Not StringIsInt($asTimePart[2]) Then Return (0)
  405.     If Not StringIsInt($asTimePart[3]) Then Return (0)
  406.     ; check if all contain valid values
  407.     $asTimePart[1] = Number($asTimePart[1])    
  408.     $asTimePart[2] = Number($asTimePart[2])    
  409.     $asTimePart[3] = Number($asTimePart[3])    
  410.     If $asTimePart[1] < 0 Or $asTimePart[1] > 23 Then Return (0)
  411.     If $asTimePart[2] < 0 Or $asTimePart[2] > 59 Then Return (0)
  412.     If $asTimePart[3] < 0 Or $asTimePart[3] > 59 Then Return (0)
  413.     ; we got here so date/time must be good
  414.     Return (1)
  415. EndFunc   ;==>_DateIsValid
  416.  
  417.  
  418. ;===============================================================================
  419. ;
  420. ; Function Name:  _DateIsYear()
  421. ; Description:    Checks a given number to see if it is a valid year.
  422. ; Author(s):      Jeremy Landes <jlandes at landeserve dot com>
  423. ;
  424. ;===============================================================================
  425. Func _DateIsYear($iNumber)
  426.     If StringIsInt($iNumber) Then
  427.         If StringLen($iNumber) = 4 Then
  428.             Return 1
  429.         Else
  430.             Return 0
  431.         EndIf
  432.     Else
  433.         Return 0
  434.     EndIf
  435. EndFunc   ;==>_DateIsYear
  436.  
  437. ;===============================================================================
  438. ;
  439. ; Description:      Returns previous weekday number, based on the specified day
  440. ;                   of the week.
  441. ; Parameter(s):     $iWeekdayNum - Weekday number
  442. ; Requirement(s):   None
  443. ; Return Value(s):  On Success - Previous weekday number
  444. ;                   On Failure - 0 and sets @ERROR = 1
  445. ; Author(s):        Jeremy Landes <jlandes at landeserve dot com>
  446. ; Note(s):          None
  447. ;
  448. ;===============================================================================
  449. Func _DateLastWeekdayNum($iWeekdayNum)
  450.     ;==============================================
  451.     ; Local Constant/Variable Declaration Section
  452.     ;==============================================
  453.     Local $iLastWeekdayNum
  454.     
  455.     Select
  456.         Case Not StringIsInt($iWeekdayNum)
  457.             SetError(1)
  458.             Return 0
  459.         Case $iWeekdayNum < 1 Or $iWeekdayNum > 7
  460.             SetError(1)
  461.             Return 0
  462.         Case Else
  463.             If $iWeekdayNum = 1 Then
  464.                 $iLastWeekdayNum = 7
  465.             Else
  466.                 $iLastWeekdayNum = $iWeekdayNum - 1
  467.             EndIf
  468.             
  469.             Return $iLastWeekdayNum
  470.     EndSelect
  471. EndFunc   ;==>_DateLastWeekdayNum
  472.  
  473. ;===============================================================================
  474. ;
  475. ; Description:      Returns previous month number, based on the specified month.
  476. ; Parameter(s):     $iMonthNum - Month number
  477. ; Requirement(s):   None
  478. ; Return Value(s):  On Success - Previous month number
  479. ;                   On Failure - 0 and sets @ERROR = 1
  480. ; Author(s):        Jeremy Landes <jlandes at landeserve dot com>
  481. ; Note(s):          None
  482. ;
  483. ;===============================================================================
  484. Func _DateLastMonthNum($iMonthNum)
  485.     ;==============================================
  486.     ; Local Constant/Variable Declaration Section
  487.     ;==============================================
  488.     Local $iLastMonthNum
  489.     
  490.     Select
  491.         Case Not StringIsInt($iMonthNum)
  492.             SetError(1)
  493.             Return 0
  494.         Case $iMonthNum < 1 Or $iMonthNum > 12
  495.             SetError(1)
  496.             Return 0
  497.         Case Else
  498.             If $iMonthNum = 1 Then
  499.                 $iLastMonthNum = 12
  500.             Else
  501.                 $iLastMonthNum = $iMonthNum - 1
  502.             EndIf
  503.             
  504.             $iLastMonthNum = StringFormat("%02d", $iLastMonthNum)
  505.             Return $iLastMonthNum
  506.     EndSelect
  507. EndFunc   ;==>_DateLastMonthNum
  508.  
  509. ;===============================================================================
  510. ;
  511. ; Description:      Returns previous month's year, based on the specified month
  512. ;                   and year.
  513. ; Parameter(s):     $iMonthNum - Month number
  514. ;                   $iYear     - Year
  515. ; Requirement(s):   None
  516. ; Return Value(s):  On Success - Previous month's year
  517. ;                   On Failure - 0 and sets @ERROR = 1
  518. ; Author(s):        Jeremy Landes <jlandes at landeserve dot com>
  519. ; Note(s):          None
  520. ;
  521. ;===============================================================================
  522. Func _DateLastMonthYear($iMonthNum, $iYear)
  523.     ;==============================================
  524.     ; Local Constant/Variable Declaration Section
  525.     ;==============================================
  526.     Local $iLastYear
  527.     
  528.     Select
  529.         Case Not StringIsInt($iMonthNum) Or Not StringIsInt($iYear)
  530.             SetError(1)
  531.             Return 0
  532.         Case $iMonthNum < 1 Or $iMonthNum > 12
  533.             SetError(1)
  534.             Return 0
  535.         Case Else
  536.             If $iMonthNum = 1 Then
  537.                 $iLastYear = $iYear - 1
  538.             Else
  539.                 $iLastYear = $iYear
  540.             EndIf
  541.             
  542.             $iLastYear = StringFormat("%04d", $iLastYear)
  543.             Return $iLastYear
  544.     EndSelect
  545. EndFunc   ;==>_DateLastMonthYear
  546.  
  547. ;===============================================================================
  548. ;
  549. ; Description:      Returns the name of the month, based on the specified month.
  550. ; Parameter(s):     $iMonthNum - Month number
  551. ;                   $iShort    - Format:
  552. ;                                0 = Long name of the month
  553. ;                                1 = Abbreviated name of the month
  554. ; Requirement(s):   None
  555. ; Return Value(s):  On Success - Month name
  556. ;                   On Failure - A NULL string and sets @ERROR = 1
  557. ; Author(s):        Jeremy Landes <jlandes at landeserve dot com>
  558. ; Note(s):          English only
  559. ;
  560. ;===============================================================================
  561. Func _DateMonthOfYear($iMonthNum, $iShort)
  562.     ;==============================================
  563.     ; Local Constant/Variable Declaration Section
  564.     ;==============================================
  565.     Local $aMonthOfYear[13]
  566.     
  567.     $aMonthOfYear[1] = "January"
  568.     $aMonthOfYear[2] = "February"
  569.     $aMonthOfYear[3] = "March"
  570.     $aMonthOfYear[4] = "April"
  571.     $aMonthOfYear[5] = "May"
  572.     $aMonthOfYear[6] = "June"
  573.     $aMonthOfYear[7] = "July"
  574.     $aMonthOfYear[8] = "August"
  575.     $aMonthOfYear[9] = "September"
  576.     $aMonthOfYear[10] = "October"
  577.     $aMonthOfYear[11] = "November"
  578.     $aMonthOfYear[12] = "December"
  579.     
  580.     Select
  581.         Case Not StringIsInt($iMonthNum) Or Not StringIsInt($iShort)
  582.             SetError(1)
  583.             Return ""
  584.         Case $iMonthNum < 1 Or $iMonthNum > 12
  585.             SetError(1)
  586.             Return ""
  587.         Case Else
  588.             Select
  589.                 Case $iShort = 0
  590.                     Return $aMonthOfYear[$iMonthNum]
  591.                 Case $iShort = 1
  592.                     Return StringLeft($aMonthOfYear[$iMonthNum], 3)
  593.                 Case Else
  594.                     SetError(1)
  595.                     Return ""
  596.             EndSelect
  597.     EndSelect
  598. EndFunc   ;==>_DateMonthOfYear
  599.  
  600. ;===============================================================================
  601. ;
  602. ; Description:      Returns next weekday number, based on the specified day of
  603. ;                   the week.
  604. ; Parameter(s):     $iWeekdayNum - Weekday number
  605. ; Requirement(s):   None
  606. ; Return Value(s):  On Success - Next weekday number
  607. ;                   On Failure - 0 and sets @ERROR = 1
  608. ; Author(s):        Jeremy Landes <jlandes at landeserve dot com>
  609. ; Note(s):          None
  610. ;
  611. ;===============================================================================
  612. Func _DateNextWeekdayNum($iWeekdayNum)
  613.     ;==============================================
  614.     ; Local Constant/Variable Declaration Section
  615.     ;==============================================
  616.     Local $iNextWeekdayNum
  617.     
  618.     Select
  619.         Case Not StringIsInt($iWeekdayNum)
  620.             SetError(1)
  621.             Return 0
  622.         Case $iWeekdayNum < 1 Or $iWeekdayNum > 7
  623.             SetError(1)
  624.             Return 0
  625.         Case Else
  626.             If $iWeekdayNum = 7 Then
  627.                 $iNextWeekdayNum = 1
  628.             Else
  629.                 $iNextWeekdayNum = $iWeekdayNum + 1
  630.             EndIf
  631.             
  632.             Return $iNextWeekdayNum
  633.     EndSelect
  634. EndFunc   ;==>_DateNextWeekdayNum
  635.  
  636. ;===============================================================================
  637. ;
  638. ; Description:      Returns next month number, based on the specified month.
  639. ; Parameter(s):     $iMonthNum - Month number
  640. ; Requirement(s):   None
  641. ; Return Value(s):  On Success - Next month number
  642. ;                   On Failure - 0 and sets @ERROR = 1
  643. ; Author(s):        Jeremy Landes <jlandes at landeserve dot com>
  644. ; Note(s):          None
  645. ;
  646. ;===============================================================================
  647. Func _DateNextMonthNum($iMonthNum)
  648.     ;==============================================
  649.     ; Local Constant/Variable Declaration Section
  650.     ;==============================================
  651.     Local $iNextMonthNum
  652.     
  653.     Select
  654.         Case Not StringIsInt($iMonthNum)
  655.             SetError(1)
  656.             Return 0
  657.         Case $iMonthNum < 1 Or $iMonthNum > 12
  658.             SetError(1)
  659.             Return 0
  660.         Case Else
  661.             If $iMonthNum = 12 Then
  662.                 $iNextMonthNum = 1
  663.             Else
  664.                 $iNextMonthNum = $iMonthNum + 1
  665.             EndIf
  666.             
  667.             $iNextMonthNum = StringFormat("%02d", $iNextMonthNum)
  668.             Return $iNextMonthNum
  669.     EndSelect
  670. EndFunc   ;==>_DateNextMonthNum
  671.  
  672. ;===============================================================================
  673. ;
  674. ; Description:      Returns next month's year, based on the specified month and
  675. ;                   year.
  676. ; Parameter(s):     $iMonthNum - Month number
  677. ;                   $iYear     - Year
  678. ; Requirement(s):   None
  679. ; Return Value(s):  On Success - Next month's year
  680. ;                   On Failure - 0 and sets @ERROR = 1
  681. ; Author(s):        Jeremy Landes <jlandes at landeserve dot com>
  682. ; Note(s):          None
  683. ;
  684. ;===============================================================================
  685. Func _DateNextMonthYear($iMonthNum, $iYear)
  686.     ;==============================================
  687.     ; Local Constant/Variable Declaration Section
  688.     ;==============================================
  689.     Local $iNextYear
  690.     
  691.     Select
  692.         Case Not StringIsInt($iMonthNum) Or Not StringIsInt($iYear)
  693.             SetError(1)
  694.             Return 0
  695.         Case $iMonthNum < 1 Or $iMonthNum > 12
  696.             SetError(1)
  697.             Return 0
  698.         Case Else
  699.             If $iMonthNum = 12 Then
  700.                 $iNextYear = $iYear + 1
  701.             Else
  702.                 $iNextYear = $iYear
  703.             EndIf
  704.             
  705.             $iNextYear = StringFormat("%04d", $iNextYear)
  706.             Return $iNextYear
  707.     EndSelect
  708. EndFunc   ;==>_DateNextMonthYear
  709.  
  710. ;===============================================================================
  711. ;
  712. ; Description:      Split Date and Time into two separateArrays.
  713. ; Parameter(s):     $sDate format "yyyy/mm/dd[ hh:mm[:ss]]"
  714. ;                    or    format "yyyy/mm/dd[Thh:mm[:ss]]"
  715. ;                    or    format "yyyy-mm-dd[ hh:mm[:ss]]"
  716. ;                    or    format "yyyy-mm-dd[Thh:mm[:ss]]"
  717. ;                    or    format "yyyy.mm.dd[ hh:mm[:ss]]"
  718. ;                    or    format "yyyy.mm.dd[Thh:mm[:ss]]"
  719. ;                   $asDatePart[4] array that contains the Date
  720. ;                   $iTimePart[4] array that contains the Time
  721. ; Requirement(s):   None
  722. ; Return Value(s):  Always 1
  723. ; Author(s):        Jos van der Zande
  724. ; Note(s):          Its expected you first do a _DateIsValid( $sDate ) for the input
  725. ;
  726. ;===============================================================================
  727. Func _DateTimeSplit($sDate, ByRef $asDatePart, ByRef $iTimePart)
  728.     Local $sDateTime
  729.     Local $x
  730.     ; split the Date and Time portion
  731.     $sDateTime = StringSplit($sDate, " T")
  732.     ; split the date portion
  733.     If $sDateTime[0] > 0 Then $asDatePart = StringSplit($sDateTime[1], "/-.")
  734.     ; split the Time portion
  735.     If $sDateTime[0] > 1 Then
  736.         $iTimePart = StringSplit($sDateTime[2], ":")
  737.         If UBound($iTimePart) < 4 Then ReDim $iTimePart[4]
  738.     Else
  739.         Dim $iTimePart[4]
  740.     EndIf
  741.     ; Ensure the arrays contain 4 values
  742.     If UBound($asDatePart) < 4 Then ReDim $asDatePart[4]
  743.     ; update the array to contain numbers not strings
  744.     For $x = 1 To 3
  745.         If StringIsInt($asDatePart[$x]) Then
  746.             $asDatePart[$x] = Number($asDatePart[$x])
  747.         Else
  748.             $asDatePart[$x] = -1
  749.         EndIf
  750.         If StringIsInt($iTimePart[$x]) Then
  751.             $iTimePart[$x] = Number($iTimePart[$x])
  752.         Else
  753.             $iTimePart[$x] = -1
  754.         EndIf
  755.     Next
  756.     Return (1)
  757. EndFunc   ;==>_DateTimeSplit
  758.  
  759. ;===============================================================================
  760. ;
  761. ; Description:      Returns the number of days since noon 4713 BC January 1.
  762. ; Parameter(s):     $Year  - Year in format YYYY
  763. ;                   $Month - Month in format MM
  764. ;                   $sDate - Day of the month format DD
  765. ; Requirement(s):   None
  766. ; Return Value(s):  On Success - Returns the Juliandate
  767. ;                   On Failure - 0  and sets @ERROR = 1
  768. ; Author(s):        Jos van der Zande / Jeremy Landes
  769. ; Note(s):          None
  770. ;
  771. ;===============================================================================
  772. Func _DateToDayValue($iYear, $iMonth, $iDay)
  773.     Local $i_aFactor
  774.     Local $i_bFactor
  775.     Local $i_cFactor
  776.     Local $i_eFactor
  777.     Local $i_fFactor
  778.     Local $iJulianDate
  779.     ; Verify If InputDate is valid
  780.     If Not _DateIsValid(StringFormat("%04d/%02d/%02d", $iYear, $iMonth, $iDay)) Then
  781.         SetError(1)
  782.         Return ("")
  783.     EndIf
  784.     If $iMonth < 3 Then
  785.         $iMonth = $iMonth + 12
  786.         $iYear = $iYear - 1
  787.     EndIf
  788.     $i_aFactor = Int($iYear / 100)
  789.     $i_bFactor = Int($i_aFactor / 4)
  790.     $i_cFactor = 2 - $i_aFactor + $i_bFactor
  791.     $i_eFactor = Int(1461 * ($iYear + 4716) / 4)
  792.     $i_fFactor = Int(153 * ($iMonth + 1) / 5)
  793.     $iJulianDate = $i_cFactor + $iDay + $i_eFactor + $i_fFactor - 1524.5
  794.     Return ($iJulianDate)
  795. EndFunc   ;==>_DateToDayValue
  796.  
  797. ;===============================================================================
  798. ;
  799. ; Description:      Returns the DayofWeek number for a given Date.  1=Sunday
  800. ; Parameter(s):     $Year
  801. ;                   $Month
  802. ;                   $day
  803. ; Requirement(s):   None
  804. ; Return Value(s):  On Success - Returns Day of the Week Range is 1 to 7 where 1=Sunday.
  805. ;                   On Failure - 0 and sets @ERROR = 1
  806. ; Author(s):        Jos van der Zande <jdeb at autoitscript dot com>
  807. ; Note(s):          None
  808. ;
  809. ;===============================================================================
  810. Func _DateToDayOfWeek($iYear, $iMonth, $iDay)
  811.     Local $i_aFactor
  812.     Local $i_yFactor
  813.     Local $i_mFactor
  814.     Local $i_dFactor
  815.     ; Verify If InputDate is valid
  816.     If Not _DateIsValid($iYear & "/" & $iMonth & "/" & $iDay) Then
  817.         SetError(1)
  818.         Return ("")
  819.     EndIf
  820.     $i_aFactor = Int((14 - $iMonth) / 12)
  821.     $i_yFactor = $iYear - $i_aFactor
  822.     $i_mFactor = $iMonth + (12 * $i_aFactor) - 2
  823.     $i_dFactor = Mod($iDay + $i_yFactor + Int($i_yFactor / 4) - Int($i_yFactor / 100) + Int($i_yFactor / 400) + Int((31 * $i_mFactor) / 12), 7)
  824.     Return ($i_dFactor + 1)
  825. EndFunc   ;==>_DateToDayOfWeek
  826.  
  827. ;===============================================================================
  828. ;
  829. ; Description:      Returns the DayofWeek number for a given Date.  0=Monday 6=Sunday
  830. ; Parameter(s):     $Year
  831. ;                   $Month
  832. ;                   $day
  833. ; Requirement(s):   None
  834. ; Return Value(s):  On Success - Returns Day of the Week Range is 0 to 6 where 0=Monday.
  835. ;                   On Failure - 0 and sets @ERROR = 1
  836. ; Author(s):        Jos van der Zande <jdeb at autoitscript dot com>
  837. ; Note(s):          None
  838. ;
  839. ;===============================================================================
  840. Func _DateToDayOfWeekISO($iYear, $iMonth, $iDay)
  841.     Local $idow = _DateToDayOfWeek($iYear, $iMonth, $iDay)
  842.     If @error Then
  843.         SetError(1)
  844.         Return ""
  845.     EndIf
  846.     If $idow >= 2 Then Return $idow - 2
  847.     Return 6
  848. EndFunc   ;==>_DateToDayOfWeekISO
  849.  
  850. ;===============================================================================
  851. ;
  852. ; Description:      Returns the name of the month, based on the specified month number.
  853. ; Parameter(s):     $iMonthNum - Month number
  854. ;                   $iShort  - Format:
  855. ;                              0 = Long name of the month
  856. ;                              1 = Abbreviated name of the month
  857. ; Requirement(s):   None
  858. ; Return Value(s):  On Success - Month name
  859. ;                   On Failure - A NULL string and sets @ERROR = 1
  860. ; Author(s):        Jason Brand <exodius at gmail dot com>
  861. ;                    (Format based on _DateDayOfWeek by Jeremy Landes)
  862. ; Note(s):          English only
  863. ;
  864. ;===============================================================================
  865. Func _DateToMonth($iMonthNum, $ishort = 0)
  866.     ;==============================================
  867.     ; Local Constant/Variable Declaration Section
  868.     ;==============================================
  869.     Local $aMonthNumber[13] = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
  870.     Local $aMonthNumberAbbrev[13] = ["", "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"]
  871.     
  872.     Select
  873.         Case Not StringIsInt($iMonthNum)
  874.             SetError(1)
  875.             Return ""
  876.         Case $iMonthNum < 1 Or $iMonthNum > 12
  877.             SetError(1)
  878.             Return ""
  879.         Case Else
  880.             Select
  881.                 Case $ishort = 0
  882.                     Return $aMonthNumber[$iMonthNum]
  883.                 Case $ishort = 1
  884.                     Return $aMonthNumberAbbrev[$iMonthNum]
  885.                 Case Else
  886.                     SetError(1)
  887.                     Return ""
  888.             EndSelect
  889.     EndSelect
  890. EndFunc   ;==>_DateToMonth
  891.  
  892. ;===============================================================================
  893. ;
  894. ; Description:      Add the given days since noon 4713 BC January 1 and return the date.
  895. ; Parameter(s):     $iJulianDate    - Julian date number
  896. ;                   $Year  - Year in format YYYY
  897. ;                   $Month - Month in format MM
  898. ;                   $sDate - Day of the month format DD
  899. ; Requirement(s):   None
  900. ; Return Value(s):  On Success - Returns the Date in the parameter vars
  901. ;                   On Failure - 0  and sets @ERROR = 1
  902. ; Author(s):        Jos van der Zande
  903. ; Note(s):          None
  904. ;
  905. ;===============================================================================
  906. Func _DayValueToDate($iJulianDate, ByRef $iYear, ByRef $iMonth, ByRef $iDay)
  907.     Local $i_zFactor
  908.     Local $i_wFactor
  909.     Local $i_aFactor
  910.     Local $i_bFactor
  911.     Local $i_xFactor
  912.     Local $i_cFactor
  913.     Local $i_dFactor
  914.     Local $i_eFactor
  915.     Local $i_fFactor
  916.     ; check for valid input date
  917.     If $iJulianDate < 0 Or Not IsNumber($iJulianDate) Then
  918.         SetError(1)
  919.         Return 0
  920.     EndIf
  921.     ; calculte the date
  922.     $i_zFactor = Int($iJulianDate + 0.5)
  923.     $i_wFactor = Int(($i_zFactor - 1867216.25) / 36524.25)
  924.     $i_xFactor = Int($i_wFactor / 4)
  925.     $i_aFactor = $i_zFactor + 1 + $i_wFactor - $i_xFactor
  926.     $i_bFactor = $i_aFactor + 1524
  927.     $i_cFactor = Int(($i_bFactor - 122.1) / 365.25)
  928.     $i_dFactor = Int(365.25 * $i_cFactor)
  929.     $i_eFactor = Int(($i_bFactor - $i_dFactor) / 30.6001)
  930.     $i_fFactor = Int(30.6001 * $i_eFactor)
  931.     $iDay = $i_bFactor - $i_dFactor - $i_fFactor
  932.     ; (must get number less than or equal to 12)
  933.     If $i_eFactor - 1 < 13 Then
  934.         $iMonth = $i_eFactor - 1
  935.     Else
  936.         $iMonth = $i_eFactor - 13
  937.     EndIf
  938.     If $iMonth < 3 Then
  939.         $iYear = $i_cFactor - 4715    ; (if Month is January or February)
  940.     Else
  941.         $iYear = $i_cFactor - 4716    ;(otherwise)
  942.     EndIf
  943.     $iYear = StringFormat("%04d", $iYear)
  944.     $iMonth = StringFormat("%02d", $iMonth)
  945.     $iDay = StringFormat("%02d", $iDay)
  946.     Return $iYear & "/" & $iMonth & "/" & $iDay
  947. EndFunc   ;==>_DayValueToDate
  948.  
  949. ;===============================================================================
  950. ;
  951. ; Description:      Returns the date in the PC's regional settings format.
  952. ; Parameter(s):     $date - format "YYYY/MM/DD"
  953. ;                   $sType - :
  954. ;                      0 - Display a date and/or time. If there is a date part, display it as a short date.
  955. ;                          If there is a time part, display it as a long time. If present, both parts are displayed.
  956. ;                      1 - Display a date using the long date format specified in your computer's regional settings.
  957. ;                      2 - Display a date using the short date format specified in your computer's regional settings.
  958. ;                      3 - Display a time using the time format specified in your computer's regional settings.
  959. ;                      4 - Display a time using the 24-hour format (hh:mm).
  960. ; Requirement(s):   None
  961. ; Return Value(s):  On Success - Returns date in proper format
  962. ;                   On Failure - 0  and Set
  963. ;                                   @ERROR to:  1 - Invalid $sDate
  964. ;                                               2 - Invalid $sType
  965. ; Author(s):        Jos van der Zande <jdeb at autoitscript dot com>
  966. ; Note(s):          None...
  967. ;
  968. ;===============================================================================
  969. Func _DateTimeFormat($sDate, $sType)
  970.     Local $asDatePart[4]
  971.     Local $asTimePart[4]
  972.     Local $sTempDate = ""
  973.     Local $sTempTime = ""
  974.     Local $sAM
  975.     Local $sPM
  976.     Local $iWday
  977.     Local $lngX
  978.     ; Verify If InputDate is valid
  979.     If Not _DateIsValid($sDate) Then
  980.         SetError(1)
  981.         Return ("")
  982.     EndIf
  983.     ; input validation
  984.     If $sType < 0 Or $sType > 5 Or Not IsInt($sType) Then
  985.         SetError(2)
  986.         Return ("")
  987.     EndIf
  988.     ; split the date and time into arrays
  989.     _DateTimeSplit($sDate, $asDatePart, $asTimePart)
  990.     ;
  991.     ;     Const $LOCALE_USER_DEFAULT = 0x400
  992.     ;   Const $LOCALE_SDATE = 0x1D            ;  date separator
  993.     ;   Const $LOCALE_STIME = 0x1E            ;  time separator
  994.     ;   Const $LOCALE_S1159 = 0x28            ;  AM designator
  995.     ;   Const $LOCALE_S2359 = 0x29            ;  PM designator
  996.     ;     Const $LOCALE_SSHORTDATE = 0x1F       ;  short date format string
  997.     ;     Const $LOCALE_SLONGDATE = 0x20        ;  long date format string
  998.     ;     Const $LOCALE_STIMEFORMAT = 0x1003    ;  time format string
  999.     
  1000.     Switch $sType
  1001.         Case 0
  1002.             ; Get ShortDate format
  1003.             $lngX = DllCall("kernel32.dll", "long", "GetLocaleInfo", "long", 0x400, "long", 0x1F, "str", "", "long", 255)
  1004.             If Not @error And $lngX[0] <> 0 Then
  1005.                 $sTempDate = $lngX[3]
  1006.             Else
  1007.                 $sTempDate = "M/d/yyyy"
  1008.             EndIf
  1009.             ;
  1010.             ; Get Time format
  1011.             If $asTimePart[0] > 1 Then
  1012.                 $lngX = DllCall("kernel32.dll", "long", "GetLocaleInfo", "long", 0x400, "long", 0x1003, "str", "", "long", 255)
  1013.                 If Not @error And $lngX[0] <> 0 Then
  1014.                     $sTempTime = $lngX[3]
  1015.                 Else
  1016.                     $sTempTime = "h:mm:ss tt"
  1017.                 EndIf
  1018.             EndIf
  1019.         Case 1
  1020.             ; Get LongDate format
  1021.             $lngX = DllCall("kernel32.dll", "long", "GetLocaleInfo", "long", 0x400, "long", 0x20, "str", "", "long", 255)
  1022.             If Not @error And $lngX[0] <> 0 Then
  1023.                 $sTempDate = $lngX[3]
  1024.             Else
  1025.                 $sTempDate = "dddd, MMMM dd, yyyy"
  1026.             EndIf
  1027.         Case 2
  1028.             ; Get ShortDate format
  1029.             $lngX = DllCall("kernel32.dll", "long", "GetLocaleInfo", "long", 0x400, "long", 0x1F, "str", "", "long", 255)
  1030.             If Not @error And $lngX[0] <> 0 Then
  1031.                 $sTempDate = $lngX[3]
  1032.             Else
  1033.                 $sTempDate = "M/d/yyyy"
  1034.             EndIf
  1035.         Case 3
  1036.             ;
  1037.             ; Get Time format
  1038.             If $asTimePart[0] > 1 Then
  1039.                 $lngX = DllCall("kernel32.dll", "long", "GetLocaleInfo", "long", 0x400, "long", 0x1003, "str", "", "long", 255)
  1040.                 If Not @error And $lngX[0] <> 0 Then
  1041.                     $sTempTime = $lngX[3]
  1042.                 Else
  1043.                     $sTempTime = "h:mm:ss tt"
  1044.                 EndIf
  1045.             EndIf
  1046.         Case 4
  1047.             If $asTimePart[0] > 1 Then
  1048.                 $sTempTime = "hh:mm"
  1049.             EndIf
  1050.         Case 5
  1051.             If $asTimePart[0] > 1 Then
  1052.                 $sTempTime = "hh:mm:ss"
  1053.             EndIf
  1054.     EndSwitch
  1055.     ; Format DATE
  1056.     If $sTempDate <> "" Then
  1057.         ;   Const $LOCALE_SDATE = 0x1D            ;  date separator
  1058.         $lngX = DllCall("kernel32.dll", "long", "GetLocaleInfo", "long", 0x400, "long", 0x1D, "str", "", "long", 255)
  1059.         If Not @error And $lngX[0] <> 0 Then
  1060.             $sTempTime = StringReplace($sTempTime, "/", $lngX[3])
  1061.         EndIf
  1062.         $iWday = _DateToDayOfWeek($asDatePart[1], $asDatePart[2], $asDatePart[3])
  1063.         $asDatePart[3] = StringRight("0" & $asDatePart[3], 2) ; make sure the length is 2
  1064.         $asDatePart[2] = StringRight("0" & $asDatePart[2], 2) ; make sure the length is 2
  1065.         $sTempDate = StringReplace($sTempDate, "d", "@")
  1066.         $sTempDate = StringReplace($sTempDate, "m", "#")
  1067.         $sTempDate = StringReplace($sTempDate, "y", "&")
  1068.         $sTempDate = StringReplace($sTempDate, "@@@@", _DateDayOfWeek($iWday, 0))
  1069.         $sTempDate = StringReplace($sTempDate, "@@@", _DateDayOfWeek($iWday, 1))
  1070.         $sTempDate = StringReplace($sTempDate, "@@", $asDatePart[3])
  1071.         $sTempDate = StringReplace($sTempDate, "@", StringReplace(StringLeft($asDatePart[3], 1), "0", "") & StringRight($asDatePart[3], 1))
  1072.         $sTempDate = StringReplace($sTempDate, "####", _DateMonthOfYear($asDatePart[2], 0))
  1073.         $sTempDate = StringReplace($sTempDate, "###", _DateMonthOfYear($asDatePart[2], 1))
  1074.         $sTempDate = StringReplace($sTempDate, "##", $asDatePart[2])
  1075.         $sTempDate = StringReplace($sTempDate, "#", StringReplace(StringLeft($asDatePart[2], 1), "0", "") & StringRight($asDatePart[2], 1))
  1076.         $sTempDate = StringReplace($sTempDate, "&&&&", $asDatePart[1])
  1077.         $sTempDate = StringReplace($sTempDate, "&&", StringRight($asDatePart[1], 2))
  1078.     EndIf
  1079.     ; Format TIME
  1080.     If $sTempTime <> "" Then
  1081.         $lngX = DllCall("kernel32.dll", "long", "GetLocaleInfo", "long", 0x400, "long", 0x28, "str", "", "long", 255)
  1082.         If Not @error And $lngX[0] <> 0 Then
  1083.             $sAM = $lngX[3]
  1084.         Else
  1085.             $sAM = "AM"
  1086.         EndIf
  1087.         $lngX = DllCall("kernel32.dll", "long", "GetLocaleInfo", "long", 0x400, "long", 0x29, "str", "", "long", 255)
  1088.         If Not @error And $lngX[0] <> 0 Then
  1089.             $sPM = $lngX[3]
  1090.         Else
  1091.             $sPM = "PM"
  1092.         EndIf
  1093.         ;   Const $LOCALE_STIME = 0x1E            ;  time separator
  1094.         $lngX = DllCall("kernel32.dll", "long", "GetLocaleInfo", "long", 0x400, "long", 0x1E, "str", "", "long", 255)
  1095.         If Not @error And $lngX[0] <> 0 Then
  1096.             $sTempTime = StringReplace($sTempTime, ":", $lngX[3])
  1097.         EndIf
  1098.         If StringInStr($sTempTime, "tt") Then
  1099.             If $asTimePart[1] < 12 Then
  1100.                 $sTempTime = StringReplace($sTempTime, "tt", $sAM)
  1101.                 If $asTimePart[1] = 0 Then $asTimePart[1] = 12
  1102.             Else
  1103.                 $sTempTime = StringReplace($sTempTime, "tt", $sPM)
  1104.                 If $asTimePart[1] > 12 Then $asTimePart[1] = $asTimePart[1] - 12
  1105.             EndIf
  1106.         EndIf
  1107.         $asTimePart[1] = StringRight("0" & $asTimePart[1], 2) ; make sure the length is 2
  1108.         $asTimePart[2] = StringRight("0" & $asTimePart[2], 2) ; make sure the length is 2
  1109.         $asTimePart[3] = StringRight("0" & $asTimePart[3], 2) ; make sure the length is 2
  1110.         $sTempTime = StringReplace($sTempTime, "hh", StringFormat("%02d", $asTimePart[1]))
  1111.         $sTempTime = StringReplace($sTempTime, "h", StringReplace(StringLeft($asTimePart[1], 1), "0", "") & StringRight($asTimePart[1], 1))
  1112.         $sTempTime = StringReplace($sTempTime, "mm", StringFormat("%02d", $asTimePart[2]))
  1113.         $sTempTime = StringReplace($sTempTime, "ss", StringFormat("%02d", $asTimePart[3]))
  1114.         $sTempDate = StringStripWS($sTempDate & " " & $sTempTime, 3)
  1115.     EndIf
  1116.     Return ($sTempDate)
  1117. EndFunc   ;==>_DateTimeFormat
  1118.  
  1119. ;===============================================================================
  1120. ;
  1121. ; Description:      Returns the the julian date in format YYDDD
  1122. ; Parameter(s):     $iJulianDate    - Julian date number
  1123. ;                   $Year  - Year in format YYYY
  1124. ;                   $Month - Month in format MM
  1125. ;                   $sDate - Day of the month format DD
  1126. ; Requirement(s):   None
  1127. ; Return Value(s):  On Success - Returns the Date in the parameter vars
  1128. ;                   On Failure - 0  and sets @ERROR = 1
  1129. ; Author(s):        Jeremy Landes / Jos van der Zande
  1130. ; Note(s):          None
  1131. ;
  1132. ;===============================================================================
  1133. Func _DateJulianDayNo($iYear, $iMonth, $iDay)
  1134.     Local $sFullDate
  1135.     Local $aiDaysInMonth
  1136.     Local $iJDay
  1137.     Local $iCntr
  1138.     ; Verify If InputDate is valid
  1139.     $sFullDate = StringFormat("%04d/%02d/%02d", $iYear, $iMonth, $iDay)
  1140.     If Not _DateIsValid($sFullDate) Then
  1141.         SetError(1)
  1142.         Return ""
  1143.     EndIf
  1144.     ; Build JDay value
  1145.     $iJDay = 0
  1146.     $aiDaysInMonth = __DaysInMonth($iYear)
  1147.     For $iCntr = 1 To $iMonth - 1
  1148.         $iJDay = $iJDay + $aiDaysInMonth[$iCntr]
  1149.     Next
  1150.     $iJDay = ($iYear * 1000) + ($iJDay + $iDay)
  1151.     Return $iJDay
  1152. EndFunc   ;==>_DateJulianDayNo
  1153.  
  1154. ;===============================================================================
  1155. ;
  1156. ; Description:      Returns the date for a julian date in format YYDDD
  1157. ; Parameter(s):     $iJDate  - Julian date number
  1158. ; Requirement(s):   None
  1159. ; Return Value(s):  On Success - Returns the Date in format YYYY/MM/DD
  1160. ;                   On Failure - 0  and sets @ERROR = 1
  1161. ; Author(s):        Jeremy Landes / Jos van der Zande
  1162. ; Note(s):          None
  1163. ;
  1164. ;===============================================================================
  1165. Func _JulianToDate($iJDay, $sSep = "/")
  1166.     Local $aiDaysInMonth
  1167.     Local $iYear
  1168.     Local $iMonth
  1169.     Local $iDay
  1170.     Local $iDays
  1171.     Local $iMaxDays
  1172.     ; Verify If InputDate is valid
  1173.     $iYear = Int($iJDay / 1000)
  1174.     $iDays = Mod($iJDay, 1000)
  1175.     $iMaxDays = 365
  1176.     If _DateIsLeapYear($iYear) Then $iMaxDays = 366
  1177.     If $iDays > $iMaxDays Then
  1178.         SetError(1)
  1179.         Return ""
  1180.     EndIf
  1181.     ; Convert to regular date
  1182.     $aiDaysInMonth = __DaysInMonth($iYear)
  1183.     $iMonth = 1
  1184.     While $iDays > $aiDaysInMonth[ $iMonth ]
  1185.         $iDays = $iDays - $aiDaysInMonth[ $iMonth ]
  1186.         $iMonth = $iMonth + 1
  1187.     WEnd
  1188.     $iDay = $iDays
  1189.     Return StringFormat("%04d%s%02d%s%02d", $iYear, $sSep, $iMonth, $sSep, $iDay)
  1190. EndFunc   ;==>_JulianToDate
  1191.  
  1192. ;===============================================================================
  1193. ;
  1194. ; Description:      Returns the current Date and Time in the pc's format
  1195. ; Parameter(s):     None
  1196. ; Requirement(s):   None
  1197. ; Return Value(s):  On Success - Date in pc's format
  1198. ; Author(s):        Jos van der Zande
  1199. ; Note(s):          None
  1200. ;
  1201. ;===============================================================================
  1202. Func _Now()
  1203.     Return (_DateTimeFormat(@YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC, 0))
  1204. EndFunc   ;==>_Now
  1205.  
  1206. ;===============================================================================
  1207. ;
  1208. ; Description:      Returns the current Date and Time in format YYYY/MM/DD HH:MM:SS
  1209. ; Parameter(s):     None
  1210. ; Requirement(s):   None
  1211. ; Return Value(s):  On Success - Date in in format YYYY/MM/DD HH:MM:SS
  1212. ; Author(s):        Jos van der Zande
  1213. ; Note(s):          None
  1214. ;
  1215. ;===============================================================================
  1216. Func _NowCalc()
  1217.     Return (@YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC)
  1218. EndFunc   ;==>_NowCalc
  1219. ;===============================================================================
  1220. ;
  1221. ; Description:      Returns the current Date in format YYYY/MM/DD
  1222. ; Parameter(s):     None
  1223. ; Requirement(s):   None
  1224. ; Return Value(s):  On Success - Date in in format YYYY/MM/DD
  1225. ; Author(s):        Jos van der Zande
  1226. ; Note(s):          None
  1227. ;
  1228. ;===============================================================================
  1229. Func _NowCalcDate()
  1230.     Return (@YEAR & "/" & @MON & "/" & @MDAY)
  1231. EndFunc   ;==>_NowCalcDate
  1232.  
  1233. ;===============================================================================
  1234. ;
  1235. ; Description:      Returns the current Date in the pc's format
  1236. ; Parameter(s):     None
  1237. ; Requirement(s):   None
  1238. ; Return Value(s):  On Success - Date in pc's format
  1239. ; Author(s):        Jos van der Zande (Larry's idea)
  1240. ; Note(s):          None
  1241. ;
  1242. ;===============================================================================
  1243. Func _NowDate()
  1244.     Return (_DateTimeFormat(@YEAR & "/" & @MON & "/" & @MDAY, 0))
  1245. EndFunc   ;==>_NowDate
  1246.  
  1247. ;===============================================================================
  1248. ;
  1249. ; Description:      Returns the current Date and Time in the pc's format
  1250. ; Parameter(s):     None
  1251. ; Requirement(s):   None
  1252. ; Return Value(s):  On Success - Date in pc's format
  1253. ; Author(s):        Jos van der Zande
  1254. ; Note(s):          None
  1255. ;
  1256. ;===============================================================================
  1257. Func _NowTime($sType = 3)
  1258.     If $sType < 3 Or $sType > 5 Then $sType = 3
  1259.     Return (_DateTimeFormat(@YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC, $sType))
  1260. EndFunc   ;==>_NowTime
  1261.  
  1262. ;===============================================================================
  1263. ;
  1264. ; Description:      Sets the local date of the system / computer
  1265. ; Parameter(s):     $iDay     - Day of month Values: 1-31
  1266. ;                   $iMonth - Moth Values: 1-12
  1267. ;                   $iYear (optional)  - Year Values: > 0 (windows might restrict this further!!)
  1268. ;
  1269. ; Requirement(s):   DllCall
  1270. ; Return Value(s):  On Success - 1
  1271. ;                   On Failure - 0 sets @ERROR = 1 and @EXTENDED (Windows API error code)
  1272. ;
  1273. ; Error code(s):     http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/system_error_codes.asp
  1274. ;
  1275. ; Author(s):        /dev/null
  1276. ; Note(s):          -
  1277. ;
  1278. ;===============================================================================
  1279. Func _SetDate($iDay, $iMonth = 0, $iYear = 0)
  1280.     
  1281.     Local $iRetval, $SYSTEMTIME, $lpSystemTime
  1282.     
  1283.     ;============================================================================
  1284.     ;== Some error checking
  1285.     ;============================================================================
  1286.     If $iYear = 0 Then $iYear = @YEAR
  1287.     If $iMonth = 0 Then $iMonth = @MON
  1288.     If Not _DateIsValid($iYear & "/" & $iMonth & "/" & $iDay) Then Return 1
  1289.     
  1290.     $SYSTEMTIME = DllStructCreate("ushort;ushort;ushort;ushort;ushort;ushort;ushort;ushort")
  1291.     $lpSystemTime = DllStructGetPtr($SYSTEMTIME)
  1292.     
  1293.     ;============================================================================
  1294.     ;== Get the local system time to fill up the SYSTEMTIME structure
  1295.     ;============================================================================
  1296.     $iRetval = DllCall("kernel32.dll", "long", "GetLocalTime", "ptr", $lpSystemTime)
  1297.     
  1298.     ;============================================================================
  1299.     ;== Change the necessary values
  1300.     ;============================================================================
  1301.     DllStructSetData($SYSTEMTIME, 4, $iDay)
  1302.     If $iMonth > 0 Then DllStructSetData($SYSTEMTIME, 2, $iMonth)
  1303.     If $iYear > 0 Then DllStructSetData($SYSTEMTIME, 1, $iYear)
  1304.     
  1305.     ;============================================================================
  1306.     ;== Set the new date
  1307.     ;============================================================================
  1308.     $iRetval = DllCall("kernel32.dll", "long", "SetLocalTime", "ptr", $lpSystemTime)
  1309.     ; a second call is needed to take care of daylight saving see MSDN
  1310.     $iRetval = DllCall("kernel32.dll", "long", "SetLocalTime", "ptr", $lpSystemTime)
  1311.     
  1312.     ;============================================================================
  1313.     ;== If DllCall was successfull, check for an error of the API Call
  1314.     ;============================================================================
  1315.     If @error = 0 Then
  1316.         If $iRetval[0] = 0 Then
  1317.             Local $lastError = DllCall("kernel32.dll", "int", "GetLastError")
  1318.             SetExtended($lastError[0])
  1319.             SetError(1)
  1320.             Return 0
  1321.         Else
  1322.             Return 1
  1323.         EndIf
  1324.         ;============================================================================
  1325.         ;== If DllCall was UNsuccessfull, return an error
  1326.         ;============================================================================
  1327.     Else
  1328.         SetError(1)
  1329.         Return 0
  1330.     EndIf
  1331.     
  1332. EndFunc   ;==>_SetDate
  1333.  
  1334. ;===============================================================================
  1335. ;
  1336. ; Description:      Sets the local time of the system / computer
  1337. ; Parameter(s):     $iHour     - hour Values: 0-23
  1338. ;                   $iMinute - minute Values: 0-59
  1339. ;                   $iSecond (optional)  - second Values: 0-59
  1340. ;
  1341. ; Requirement(s):   DllCall
  1342. ; Return Value(s):  On Success - 1
  1343. ;                   On Failure - 0 sets @ERROR = 1 and @EXTENDED (Windows API error code)
  1344. ;
  1345. ; Error code(s):     http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/system_error_codes.asp
  1346. ;
  1347. ; Author(s):        /dev/null
  1348. ; Note(s):          -
  1349. ;
  1350. ;===============================================================================
  1351. Func _SetTime($iHour, $iMinute, $iSecond = 0)
  1352.     
  1353.     Local $iRetval, $SYSTEMTIME, $lpSystemTime
  1354.     
  1355.     ;============================================================================
  1356.     ;== Some error checking
  1357.     ;============================================================================
  1358.     If $iHour < 0 Or $iHour > 23 Then Return 1
  1359.     If $iMinute < 0 Or $iMinute > 59 Then Return 1
  1360.     If $iSecond < 0 Or $iSecond > 59 Then Return 1
  1361.     
  1362.     $SYSTEMTIME = DllStructCreate("ushort;ushort;ushort;ushort;ushort;ushort;ushort;ushort")
  1363.     $lpSystemTime = DllStructGetPtr($SYSTEMTIME)
  1364.     
  1365.     ;============================================================================
  1366.     ;== Get the local system time to fill up the SYSTEMTIME structure
  1367.     ;============================================================================
  1368.     $iRetval = DllCall("kernel32.dll", "long", "GetLocalTime", "ptr", $lpSystemTime)
  1369.     
  1370.     ;============================================================================
  1371.     ;== Change the necessary values
  1372.     ;============================================================================
  1373.     DllStructSetData($SYSTEMTIME, 5, $iHour)
  1374.     DllStructSetData($SYSTEMTIME, 6, $iMinute)
  1375.     If $iSecond > 0 Then DllStructSetData($SYSTEMTIME, 7, $iSecond)
  1376.     
  1377.     ;============================================================================
  1378.     ;== Set the new time
  1379.     ;============================================================================
  1380.     $iRetval = DllCall("kernel32.dll", "long", "SetLocalTime", "ptr", $lpSystemTime)
  1381.     ; a second call is needed to take care of daylight saving see MSDN
  1382.     $iRetval = DllCall("kernel32.dll", "long", "SetLocalTime", "ptr", $lpSystemTime)
  1383.     
  1384.     ;============================================================================
  1385.     ;== If DllCall was successfull, check for an error of the API Call
  1386.     ;============================================================================
  1387.     If @error = 0 Then
  1388.         If $iRetval[0] = 0 Then
  1389.             Local $lastError = DllCall("kernel32.dll", "int", "GetLastError")
  1390.             SetExtended($lastError[0])
  1391.             SetError(1)
  1392.             Return 0
  1393.         Else
  1394.             Return 1
  1395.         EndIf
  1396.         ;============================================================================
  1397.         ;== If DllCall was UNsuccessfull, return an error
  1398.         ;============================================================================
  1399.     Else
  1400.         SetError(1)
  1401.         Return 0
  1402.     EndIf
  1403.     
  1404. EndFunc   ;==>_SetTime
  1405.  
  1406. ;===============================================================================
  1407. ;
  1408. ; Description:      Converts the specified tick amount to hours, minutes, and
  1409. ;                   seconds.
  1410. ; Parameter(s):     $iTicks - Tick amount
  1411. ;                   $iHours - Variable to store the hours (ByRef)
  1412. ;                   $iMins  - Variable to store the minutes (ByRef)
  1413. ;                   $iSecs  - Variable to store the seconds (ByRef)
  1414. ; Requirement(s):   None
  1415. ; Return Value(s):  On Success - 1
  1416. ;                   On Failure - 0 and sets @ERROR = 1
  1417. ; Author(s):        Marc <mrd at gmx de>
  1418. ; Note(s):          None
  1419. ;
  1420. ;===============================================================================
  1421. Func _TicksToTime($iTicks, ByRef $iHours, ByRef $iMins, ByRef $iSecs)
  1422.     If Number($iTicks) > 0 Then
  1423.         $iTicks = Round($iTicks / 1000)
  1424.         $iHours = Int($iTicks / 3600)
  1425.         $iTicks = Mod($iTicks, 3600)
  1426.         $iMins = Int($iTicks / 60)
  1427.         $iSecs = Round(Mod($iTicks, 60))
  1428.         ; If $iHours = 0 then $iHours = 24
  1429.         Return 1
  1430.     ElseIf Number($iTicks) = 0 Then
  1431.         $iHours = 0
  1432.         $iTicks = 0
  1433.         $iMins = 0
  1434.         $iSecs = 0
  1435.         Return 1
  1436.     Else
  1437.         SetError(1)
  1438.         Return 0
  1439.     EndIf
  1440. EndFunc   ;==>_TicksToTime
  1441.  
  1442. ;===============================================================================
  1443. ;
  1444. ; Description:      Converts the specified hours, minutes, and seconds to ticks.
  1445. ; Parameter(s):     $iHours - Hours
  1446. ;                   $iMins  - Minutes
  1447. ;                   $iSecs  - Seconds
  1448. ; Requirement(s):   None
  1449. ; Return Value(s):  On Success - Converted tick amount
  1450. ;                   On Failure - 0 and sets @ERROR = 1
  1451. ; Author(s):        Marc <mrd at gmx de>
  1452. ;                   SlimShady: added the default time and made parameters optional
  1453. ; Note(s):          None
  1454. ;
  1455. ;===============================================================================
  1456. Func _TimeToTicks($iHours = @HOUR, $iMins = @MIN, $iSecs = @SEC)
  1457.     ;==============================================
  1458.     ; Local Constant/Variable Declaration Section
  1459.     ;==============================================
  1460.     Local $iTicks
  1461.     
  1462.     If StringIsInt($iHours) And StringIsInt($iMins) And StringIsInt($iSecs) Then
  1463.         $iTicks = 1000 * ((3600 * $iHours) + (60 * $iMins) + $iSecs)
  1464.         Return $iTicks
  1465.     Else
  1466.         SetError(1)
  1467.         Return 0
  1468.     EndIf
  1469. EndFunc   ;==>_TimeToTicks
  1470.  
  1471. ;===============================================================================
  1472. ;
  1473. ; Function Name:    _WeekNumberISO()
  1474. ; Description:      Find out the week number of current date OR date given in parameters
  1475. ;
  1476. ; Parameter(s):     $iDay    - Day value (default = current day)
  1477. ;                   $iMonth    - Month value (default = current month)
  1478. ;                   $iYear    - Year value (default = current year)
  1479. ; Requirement(s):
  1480. ; Return Value(s):  On Success     - Returns week number of given date
  1481. ;                   On Failure     - returns -1  and sets @ERROR = 1 on faulty parameters values
  1482. ;                   On non-acceptable weekstart value sets @ERROR = 99 and uses default (Sunday) as starting day
  1483. ; Author(s):        Tuape
  1484. ;                   JdeB: modified to UDF standards & Doc.
  1485. ;                   JdeB: Change calculation logic.
  1486. ;===============================================================================
  1487. ;
  1488. Func _WeekNumberISO($iYear = @YEAR, $iMonth = @MON, $iDay = @MDAY)
  1489.     Local $idow, $iDow0101
  1490.     
  1491.     ; Check for erroneous input in $Day, $Month & $Year
  1492.     If $iDay > 31 Or $iDay < 1 Then
  1493.         SetError(1)
  1494.         Return -1
  1495.     ElseIf $iMonth > 12 Or $iMonth < 1 Then
  1496.         SetError(1)
  1497.         Return -1
  1498.     ElseIf $iYear < 1 Or $iYear > 2999 Then
  1499.         SetError(1)
  1500.         Return -1
  1501.     EndIf
  1502.     
  1503.     $idow = _DateToDayOfWeekISO($iYear, $iMonth, $iDay);
  1504.     $iDow0101 = _DateToDayOfWeekISO($iYear, 1, 1);
  1505.     
  1506.     If ($iMonth = 1 And 3 < $iDow0101 And $iDow0101 < 7 - ($iDay - 1)) Then
  1507.         ;days before week 1 of the current year have the same week number as
  1508.         ;the last day of the last week of the previous year
  1509.         $idow = $iDow0101 - 1;
  1510.         $iDow0101 = _DateToDayOfWeekISO($iYear - 1, 1, 1);
  1511.         $iMonth = 12
  1512.         $iDay = 31
  1513.         $iYear = $iYear - 1
  1514.     ElseIf ($iMonth = 12 And 30 - ($iDay - 1) < _DateToDayOfWeekISO($iYear + 1, 1, 1) And _DateToDayOfWeekISO($iYear + 1, 1, 1) < 4) Then
  1515.         ; days after the last week of the current year have the same week number as
  1516.         ; the first day of the next year, (i.e. 1)
  1517.         Return 1;
  1518.     EndIf
  1519.     
  1520.     Return Int((_DateToDayOfWeekISO($iYear, 1, 1) < 4) + 4 * ($iMonth - 1) + (2 * ($iMonth - 1) + ($iDay - 1) + $iDow0101 - $idow + 6) * 36 / 256)
  1521.     
  1522. EndFunc   ;==>_WeekNumberISO
  1523.  
  1524.  
  1525. ;===============================================================================
  1526. ;
  1527. ; Function Name:    _WeekNumber()
  1528. ; Description:      Find out the week number of current date OR date given in parameters
  1529. ;
  1530. ; Parameter(s):     $iDay    - Day value (default = current day)
  1531. ;                   $iMonth    - Month value (default = current month)
  1532. ;                   $iYear    - Year value (default = current year)
  1533. ;                   $iWeekstart - Week starts from Sunday (1, default) or Monday (2)
  1534. ; Requirement(s):
  1535. ; Return Value(s):  On Success     - Returns week number of given date
  1536. ;                   On Failure     - returns -1  and sets @ERROR = 1 on faulty parameters values
  1537. ;                   On non-acceptable weekstart value sets @ERROR = 99 and uses default (Sunday) as starting day
  1538. ; Author(s):        JdeB
  1539. ;===============================================================================
  1540. ;
  1541. Func _WeekNumber($iYear = @YEAR, $iMonth = @MON, $iDay = @MDAY, $iWeekStart = 1)
  1542.     Local $iDow0101, $iDow0101ny
  1543.     Local $iDate, $iStartWeek1, $iEndWeek1, $iEndWeek1Date, $iStartWeek1ny, $iStartWeek1Dateny
  1544.     Local $iCurrDateDiff, $iCurrDateDiffny
  1545.     
  1546.     ; Check for erroneous input in $Day, $Month & $Year
  1547.     If $iDay > 31 Or $iDay < 1 Then
  1548.         SetError(1)
  1549.         Return -1
  1550.     ElseIf $iMonth > 12 Or $iMonth < 1 Then
  1551.         SetError(1)
  1552.         Return -1
  1553.     ElseIf $iYear < 1 Or $iYear > 2999 Then
  1554.         SetError(1)
  1555.         Return -1
  1556.     ElseIf $iWeekStart < 1 Or $iWeekStart > 2 Then
  1557.         SetError(2)
  1558.         Return -1
  1559.     EndIf
  1560.     ;
  1561.     ;$idow = _DateToDayOfWeekISO($iYear, $iMonth, $iDay);
  1562.     $iDow0101 = _DateToDayOfWeekISO($iYear, 1, 1);
  1563.     $iDate = $iYear & '/' & $iMonth & '/' & $iDay
  1564.     ;Calculate the Start and End date of Week 1 this year
  1565.     If $iWeekStart = 1 Then
  1566.         If $iDow0101 = 6 Then
  1567.             $iStartWeek1 = 0
  1568.         Else
  1569.             $iStartWeek1 = -1 * $iDow0101 - 1
  1570.         EndIf
  1571.         $iEndWeek1 = $iStartWeek1 + 6
  1572.     Else
  1573.         $iStartWeek1 = $iDow0101 * - 1
  1574.         $iEndWeek1 = $iStartWeek1 + 6
  1575.     EndIf
  1576.     ;$iStartWeek1Date = _DateAdd('d',$iStartWeek1,$iYear & '/01/01')
  1577.     $iEndWeek1Date = _DateAdd('d', $iEndWeek1, $iYear & '/01/01')
  1578.     ;Calculate the Start and End date of Week 1 this Next year
  1579.     $iDow0101ny = _DateToDayOfWeekISO($iYear + 1, 1, 1);
  1580.     ;  1 = start on Sunday / 2 = start on Monday
  1581.     If $iWeekStart = 1 Then
  1582.         If $iDow0101ny = 6 Then
  1583.             $iStartWeek1ny = 0
  1584.         Else
  1585.             $iStartWeek1ny = -1 * $iDow0101ny - 1
  1586.         EndIf
  1587.         ;$IEndWeek1ny = $iStartWeek1ny + 6
  1588.     Else
  1589.         $iStartWeek1ny = $iDow0101ny * - 1
  1590.         ;$IEndWeek1ny = $iStartWeek1ny + 6
  1591.     EndIf
  1592.     $iStartWeek1Dateny = _DateAdd('d', $iStartWeek1ny, $iYear + 1 & '/01/01')
  1593.     ;$iEndWeek1Dateny = _DateAdd('d',$IEndWeek1ny,$iYear+1 & '/01/01')
  1594.     ;number of days after end week 1
  1595.     $iCurrDateDiff = _DateDiff('d', $iEndWeek1Date, $iDate) - 1
  1596.     ;number of days before next week 1 start
  1597.     $iCurrDateDiffny = _DateDiff('d', $iStartWeek1Dateny, $iDate)
  1598.     ;
  1599.     ; Check for end of year
  1600.     If $iCurrDateDiff >= 0 And $iCurrDateDiffny < 0 Then Return 2 + Int($iCurrDateDiff / 7)
  1601.     ; > week 1
  1602.     If $iCurrDateDiff < 0 Or $iCurrDateDiffny >= 0 Then Return 1
  1603. EndFunc   ;==>_WeekNumber
  1604.  
  1605.  
  1606. ;===============================================================================
  1607. ;
  1608. ; Description:      returns an Array that contains the numbers of days per month
  1609. ;                   te specified year
  1610. ; Parameter(s):     $iYear
  1611. ; Requirement(s):   None
  1612. ; Return Value(s):  On Success - Array that contains the numbers of days per month
  1613. ;                   On Failure - none
  1614. ; Author(s):        Jos van der Zande / Jeremy Landes
  1615. ; Note(s):          None
  1616. ;
  1617. ;===============================================================================
  1618. Func __DaysInMonth($iYear)
  1619.     Local $aiDays
  1620.     $aiDays = StringSplit("31,28,31,30,31,30,31,31,30,31,30,31", ",")
  1621.     If _DateIsLeapYear($iYear) Then $aiDays[2] = 29
  1622.     Return $aiDays
  1623. EndFunc   ;==>__DaysInMonth
  1624.